package com.example.teamwork1.controller;


import com.example.teamwork1.model.t_user;
import com.example.teamwork1.service.MsmService;
import com.example.teamwork1.service.t_userService;
import com.example.teamwork1.util.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class LoginController {
    @Resource
    public t_userService t_userService;

    @PostMapping("/loginCheck")
    public String loginCheck(String username,String password)
    {
        return t_userService.loginCheck(username, password);
    }

    @PostMapping("/loginCheckByMail")
    public String loginCheckByMail(String mail,String code){ return  t_userService.loginCheckByMail(mail, code);}

    @PostMapping("/registerCheck")
    public String registerCheck(String username,String nickname,String password,String phone,String mail,String gender)
    {
        t_user U=new t_user();
        U.setUsername(username);
        U.setNickname(nickname);
        U.setPassword(password);
        U.setPhone(phone);
        U.setMail(mail);
        U.setGender(gender);
        return t_userService.registerCheck(U);
    }
    @PostMapping("/seekPasswordCheck")
    public String seekPasswordCheck(String username,String new_password,String mail,String CheckCode)
    {
        return t_userService.seekPasswordCheck(username,new_password,mail,CheckCode);
    }

    @GetMapping("/GetEMailCode")
    public String GetEMailCode(String mail)
    {
        String checkCode = t_userService.GetEMailCode(mail);
        if(checkCode.equals(""))
            return "获取验证码失败";
        else
            return "获取验证码成功，请根据去你的邮箱查看";
    }

    @GetMapping("/TokenCheck")
    public String TokenCheck(String token){
        if(JWTUtil.verify(token))
            return "true";
        else
            return "false";
    }


    @Autowired
    private MsmService msm;

    @GetMapping("/GetPhoneCode")
    public String send(@PathVariable String Phone)
    {

        boolean send = msm.send(Phone);
        if(send){
            return "成功发送短信呢";
        }else {
            return "短信发送失败呢";
        }
    }
}
